package com.simulation.web.controller.tech;

import cn.hutool.core.bean.BeanUtil;
import com.simulation.common.core.domain.AjaxResult;
import com.simulation.web.dto.TechCollectionDTO;
import com.simulation.web.domain.TechCollection;
import com.simulation.web.service.CollectionService;
import com.simulation.web.utils.PromptConstants;
import com.simulation.web.vo.TechCollectionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("合集")
@RestController
@RequestMapping("techCollection")
@AllArgsConstructor
public class TechCollectionController {

    private final CollectionService collectionService;

    /**
     * 分页查询
     */
    @GetMapping("getPage")
    @ApiOperation("分页查询合集")
    public AjaxResult getPage(TechCollectionDTO techCollectionDTO) {
        return AjaxResult.success(collectionService.getPage(techCollectionDTO));
    }

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @RequestMapping("list")
    public List<TechCollection> listAll() {
        return collectionService.listAll();
    }

    /**
     * 根据主键查询
     *
     * @param techCollectionDTO
     * @return 返回记录，没有返回null
     */
    @GetMapping("getById")
    @ApiOperation("查询合集详情")
    public AjaxResult getById(TechCollectionDTO techCollectionDTO) {
        TechCollectionVO techCollectionVO = collectionService.getById(techCollectionDTO.getId());
        if (techCollectionVO == null) {
            return AjaxResult.warn(PromptConstants.COLLECTION_NOT_EXIST);
        }
        return AjaxResult.success(techCollectionVO);
    }    
     
    /**
     * 新增，忽略null字段
     *
     * @param techCollectionDTO 新增的记录
     * @return 返回影响行数
     */
    @PostMapping("insert")
    @ApiOperation("新增合集")
    public AjaxResult insert(@RequestBody TechCollectionDTO techCollectionDTO) {
        TechCollection techCollection = new TechCollection();
        BeanUtil.copyProperties(techCollectionDTO, techCollection);
        collectionService.insertIgnoreNull(techCollection);
        return AjaxResult.success(techCollection);
    }    
      
    /**
     * 修改，忽略null字段
     *
     * @param techCollectionDTO 修改的记录
     * @return 返回影响行数
     */
    @PostMapping("update")
    @ApiOperation("修改合集")
    public AjaxResult update(@RequestBody TechCollectionDTO techCollectionDTO) {
        TechCollection techCollection = new TechCollection();
        BeanUtil.copyProperties(techCollectionDTO, techCollection);
        collectionService.updateIgnoreNull(techCollection);
        return AjaxResult.success(techCollection);
    }
    
    /**
     * 删除记录
     *
     * @param techCollectionDTO 待删除的记录
     * @return 返回影响行数
     */
    @PostMapping("delete")
    @ApiOperation("删除合集")
    public AjaxResult delete(@RequestBody TechCollectionDTO techCollectionDTO) {
        collectionService.delete(techCollectionDTO.getId());
        return AjaxResult.success();
    }

    /**
     * 合集推荐
     * @return
     */
    @GetMapping("/recommended")
    @ApiOperation("项目推荐")
    public AjaxResult recommended() {
        return AjaxResult.success(collectionService.selectViewCount());
    }


    /**
     * 合集推荐
     * @return
     */
    @GetMapping("/copy/{id}")
    @ApiOperation("项目合集复制")
    public AjaxResult copy(@PathVariable("id") Long id){
        return AjaxResult.success(collectionService.copy(id));
    }
}